package com.example.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.UserMapper;
import com.example.pojo.User;
import com.example.service.UserService;
import org.springframework.beans.TypeMismatchException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
//业务层中使用事务
@Service
//@Transactional  //当前类下面的所有方法都会被事务管理
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    /**
     * timeout:事务的过期时间
     * readOnly:当前事务是否为只读模式，默认false，查询的话改为 true
     * propagation:事务的传播行为
     * noRollbackFor rollbackFor 出现括号内的异常，不回滚；出现括号内的异常，回滚
     */
    @Autowired
    private UserMapper userMapper;
    @Override
    //第一个参数：该事务必须在30秒内完成，第三个参数：隔离级别，设置为数据库默认的。
    @Transactional(timeout = 30,readOnly = false,isolation = Isolation.DEFAULT,
    propagation = Propagation.NESTED,//rollbackFor ={TypeMismatchException.class},
    noRollbackFor = {ClassCastException.class})  //这里算是有俩事务了.
    public void updateUser(User user) {
        user.updateById();
        int i=1/0;
        //deleteUser(user);
    }

    @Override
    @Transactional
    public void deleteUser(User user) {

    }

}
